<?php

namespace App\Http\Controllers;

use App\Models\Product\Type as TypeModel;
use App\Models\Product\Product as ProductModel;
use App\Models\Product\Cart as CartModel;
use App\Models\Product\Orderproduct as OrderproductModel;


class Product extends Controller
{
    // 首页
    public function index()
    {
        // 查询分类列表
        $typelist = TypeModel::orderBy('weigh', 'desc')->limit(8)->get();

        // 查询轮播列表，推荐
        $recommendlist = ProductModel::where([
            ['status',  '=',  '3'],
            ['flag', '=', '1']
        ])->orderBy('createtime', 'desc')->limit(5)->get();

        // 新品
        $newlist = ProductModel::where([
            ['status',  '=',  '1'],
            ['flag', '=', '1']
        ])->orderBy('createtime', 'desc')->limit(5)->get();

        // 热销
        $hotlist = ProductModel::where([
            ['status',  '=',  '2'],
            ['flag', '=', '1']
        ])->orderBy('createtime', 'desc')->limit(5)->get();

        $data = [
            'typelist' => $typelist,
            'recommendlist' => $recommendlist,
            'newlist' => $newlist,
            'hotlist' => $hotlist
        ];

        return success('', '', $data);
    }

    // 产品分类
    public function type()
    {
        // 查询分类列表
        $typelist = TypeModel::orderBy('weigh', 'desc')->get();

        return success('', '', $typelist);
    }

    // 产品列表  和分类列表共用一个
    public function tabulation()
    {
        $keywords = request('keywords', '');

        $typeid = request('typeid', 0);

        $orders = request('orders', 'createtime');

        $status = request('status', '');

        $where[] = ['flag', '1'];

        // 关键字
        if ($keywords) {
            $where[] = ['name', 'like', "%$keywords%"];
        }

        // 分类ID
        if ($typeid > 0) {
            $where[] = ['typeid', $typeid];
        }

        // 1-新品2-热销3-推荐
        if ($status) {
            $where[] = ['status', $status];
        }

        $data = ProductModel::where($where)->orderBy($orders, 'desc')->get();

        return success('', '', $data);
    }

    // 产品详情
    public function info()
    {
        $busid = request('busid', 0);
        $proid = request('proid', '');

        $productinfo = ProductModel::find($proid);

        if (!$productinfo) {
            return error('当前商品下架');
        }

        if ($busid > 0) {

            // 获取购物车数量
            $cartcount = CartModel::where(['busid' => $busid])->count();

            if ($cartcount) {
                $productinfo['cartcount'] = $cartcount;
            }
        }

        // 平均分rate
        $average =  OrderproductModel::where(['proid' => (string)$proid])->avg('rate');

        $productinfo['average'] = round($average, 1);

        // 评价详情
        $ratelist = OrderproductModel::with('business')->where('proid', $proid)->whereNotNull('comment')->get();

        $data = [
            'productinfo' => $productinfo,
            'ratelist' => $ratelist
        ];
        return success('', '', $data);
    }
}
